﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Threading;
using SSATool.Common.Logging;
using SSATool.Common.Util.Properties;

#endregion

namespace SSATool.Common.Util.Threading
{
    /// <summary>
    /// Represents a synchrnous process. This may be used for testing and
    /// mocking purposes.
    /// </summary>
    public class SyncProcess : ProcessBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="SyncProcess"/> class.
        /// </summary>
        public SyncProcess()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        /// <summary>
        /// Executes the action to run with the given data.
        /// </summary>
        /// <param name="toRun">A delegate to execute which must not be null.</param>
        /// <param name="data">Data to pass to the delegate to execute.</param>
        public override void Run<T>(Action<T> toRun, T data)
        {
            PreCondition.AssertNotNull(toRun, "toRun");

            StartObservedExecution(() =>
            {
                toRun(data);
                return null;
            } );
        }

        /// <summary>
        /// Executes the action to run with the given data.
        /// </summary>
        /// <param name="toRun">A delegate to execute which must not be null.</param>
        /// <param name="data">Data to pass to the delegate to execute.</param>
        public override void Run<TArg, TRes>(Func<TArg, TRes> toRun, TArg data)
        {
            PreCondition.AssertNotNull(toRun, "toRun");

            StartObservedExecution(() => toRun(data));
        }

        /// <summary>
        /// Executes the action to run without an argument.
        /// </summary>
        /// <param name="toRun">A delegate to execute which must not be null.</param>
        public override void Run(Action toRun)
        {
            PreCondition.AssertNotNull(toRun, "toRun");

            StartObservedExecution(() =>
            {
                toRun();
                return null;
            });
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
